package com.hardroid.workload.workcalc;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import com.hardroid.workload.utils.HardroidUtils;
import com.hardroid.workload.utils.HardroidUtils;

import android.text.format.DateUtils;
import android.util.Log;

public class ObsvDay implements Serializable {	
	
	public int dayNth;
	public Employer employer;
	public Date obsvDate;	
	public Date workStart;
	public Date workEnd;
	
	public ArrayList<Tally> lTally;
	
	/**
	 * {@link Questionaire} hari itu
	 */
	public Questionaire questionaire;
	
	/**
	 * Will construct the object at the day that the observation will be done
	 */
	public ObsvDay() {
		obsvDate = new Date();
		workStart = new Date();
		workEnd = new Date();
		lTally = new ArrayList<Tally>();
		questionaire = new Questionaire();
	}
	
	public ObsvDay(Employer employer){
		obsvDate = new Date();
		workStart = new Date();
		workEnd = new Date();
		lTally = new ArrayList<Tally>();
		questionaire = new Questionaire();
		this.employer = employer;
	}
	
	/**Copy Constructor
	 * @param day
	 */
//	public ObsvDay(ObsvDay day){
//		obsvDate = new Date(day.obsvDate.getTime());
//		workStart = new Date(day.workStart.getTime());
//		workEnd= new Date(day.workEnd.getTime());
//		lTally = HardroidUtils.copyTallyList(day.lTally);		
//		questionaire = new Questionaire(day.questionaire);	
//	}
	
	public ObsvDay(ObsvDay day, Employer e){
		obsvDate = new Date(day.obsvDate.getTime());
		workStart = new Date(day.workStart.getTime());
		workEnd= new Date(day.workEnd.getTime());
		lTally = HardroidUtils.copyTallyList(day.lTally);	
		questionaire = new Questionaire();
		employer = e;
	}
	
	public void setWorkStart(int h, int m){
		workStart.setHours(h);
		workStart.setMinutes(m);
	}
	
	public void setDate (String string){
		obsvDate = HardroidUtils.XMLDateToDate(string);
	}
	
	public void setWorkEnd(String string){
		workEnd= HardroidUtils.XMLDateToDate(string);
	}
	
	public void setWorkStart(String string){
		workStart= HardroidUtils.XMLDateToDate(string);
	}
	
	public void setWorkEnd(int h, int m){
		workEnd.setHours(h);
		workEnd.setMinutes(m);
	}

	/** Mendapatkan representasi string dari obsvervation date untuk disimpan pada xml
	 * @return
	 */
	public String getXMLSObsvDate(){
		return HardroidUtils.dateToXML(obsvDate);
	}
	
	/**Mendapatkan representasi string dari waktu mulai kerja untuk disimpan pada xml
	 * @return
	 */
	public String getXMLSWorkStart(){
		return HardroidUtils.dateToXML(workStart);
	}
	
	/**Mendapatkan representasi string dari waktu selesai kerja untuk disimpan pada xml
	 * @return
	 */
	public String getXMLSWorkEnd(){
		return HardroidUtils.dateToXML(workEnd);
	}
	
	/** Mengenerate observation time untuk hari ini, sekaligus mengisi list of tally
	 * @param ntally
	 * @param gap
	 */
	public void generateObservationTime (int ntally, int gap){
		Date date = new Date(workStart.getTime());
		
		int i =0;
		while(date.before(workEnd)){
			date = HardroidUtils.nextNMinuteDate(date, gap);;
			Tally tally = new Tally(date);									
			lTally.add(tally);
			i++;
		}
		
		Random rand = new Random();
		while (lTally.size()>ntally){
			lTally.remove(rand.nextInt(lTally.size()-1)+1);
		}			
	}
	
	/** Mendaptakan jumlah total productive
	 * @return
	 */
	public int getTotalProductive(){
		int retval = 0;
		for (Tally t : lTally){
			if (t.productive){
				retval++;
			}
		}
		return retval;
	}
	
	/** Mendaptakan jumlah total non productive
	 * @return int
	 */
	public int getTotalNonProductive(){
		int retval = 0;
		for (Tally t : lTally){
			if (t.nProductive){
				retval++;
			}
		}
		return retval;
	}
		
	public int getPercentageProductive(int n){
		return n * 100 / lTally.size();
	}
		
	
	/** Mendaptakan percentase produktivitas
	 * @return
	 */
	public int getPercentageProductive(){
		return getTotalProductive()* 100 / lTally.size();
	}
		
}
